package pe.edu.upc.dsd.rs;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Type;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;

public class MobileService {

	@GET
	@Path("/soundtrack/{usuario}")
	@Produces("application/json")
	public String getSoundTrack(@PathParam("usuario") String usuario) throws Exception {

		//Se obtiene bean desde web service REST
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://localhost:8080/rs-server-consolida/rest/Consolida/getSoudTrack/" + usuario);
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
		
		//Se transforma cadena en bean
		SoundTrack soundtrack = new SoundTrack();
		Gson gson = new Gson();
		gson.fromJson(reader.readLine(), (Type) soundtrack);
		
		// Se retorna el bean en una estructura JSON
		return gson.toJson(soundtrack); 
		
		
	}
	
	@GET
	@Path("/trailers/{usuario}")
	@Produces("application/json")
	public String getTrailers(@PathParam("usuario") String usuario) throws Exception {

		//Se obtiene bean desde web service REST
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://localhost:8080/rs-server-consolida/rest/Consolida/getTrailers/" + usuario);
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
		
		//Se transforma cadena en bean
		Trailers trailers = new Trailers();
		Gson gson = new Gson();
		gson.fromJson(reader.readLine(), (Type) trailers);
		
		// Se retorna el bean en una estructura JSON
		return gson.toJson(trailers); 
		
		
	}
	
	@GET
	@Path("/rentvideo/{usuario}")
	@Produces("application/json")
	public String getRentVideo(@PathParam("usuario") String usuario) throws Exception {

		//Se obtiene bean desde web service REST
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("http://localhost:8080/rs-server-consolida/rest/Consolida/getRentVideo/" + usuario);
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
		
		//Se transforma cadena en bean
		RentVideo rentvideo = new RentVideo();
		Gson gson = new Gson();
		gson.fromJson(reader.readLine(), (Type) rentvideo);
		
		// Se retorna el bean en una estructura JSON
		return gson.toJson(rentvideo);
		
	
	}
}